//var storage = sessionStorage; 
var startFlag=false;//开始标志
	var timeNum=100;
	var level=0;
	var timeTip=null;//显示提示定时器
	var count =new  Count(0,0,0,0,0);

	$(document).ready(function(){
		var name = sessionStorage.getItem("USERNAME");  
		var sex = sessionStorage.getItem("SEX");
		if(name!=null&&sex!=null){
			count.username=name;
			count.sex=sex;
		}
		initCountNum();
		$("#highlevel").click(function(){
			if(document.getElementById("highlevel").checked==true){				
				level=50;
                document.getElementById("simplelevel").checked=false;
			}else{				
				level=0;
			}
		});
        $("#simplelevel").click(function(){
            if(document.getElementById("simplelevel").checked==true){
                level=-1;
                document.getElementById("highlevel").checked=false;
            }else{
                level=0;
            }
        });
	});
	
	function isShowLogin(){
		if(count.username==null||count.sex==null){
			$("#dialog2").show();
			$("#username").focus();
		}
	}
	function login(){

		if(!$.trim($("#username").val())){
			$("#username").focus();
			$("#username").attr("placeholder","昵称不能为空");
		}else{
			//成功
			count.sex=$("#sex").val();
			count.username=$("#username").val();
			sessionStorage.setItem("USERNAME", count.username);
			sessionStorage.setItem("SEX", count.sex);
			//alert(count.sex+":"+count.username);
			$("#dialog2").hide();
			countResult(false);
            $("#userResult").blur();//先失去焦点，保证触发数字键盘
			$("#userResult").focus();
		}
	}
	
	function initCountNum(){
		
		var operations= Math.floor(Math.random()*3);
		count.firNum = getRandomNum(73+level);		
		count.sedNum = getRandomNum(67+level);
		if(level == -1){
		    //简单模式
           operations= Math.floor(Math.random()*2);
           count.firNum = getRandomNum(21);
           count.sedNum = getRandomNum(13);
        }
		switch(operations)
		{
		case 0:
			$("#operate").html('+');
			count.result=count.firNum + count.sedNum;
		    break;
		case 1:
			$("#operate").html('-');
			if(count.sedNum>count.firNum){
				count.firNum=count.firNum + count.sedNum;
				count.sedNum=count.firNum-count.sedNum;
				count.firNum=count.firNum-count.sedNum;
			}
			count.result=count.firNum - count.sedNum;
		    break;
		case 2:
			$("#operate").html('x');
			count.firNum = getRandomNum(15+level);		
			count.sedNum = getRandomNum(12+level);
			count.result=count.firNum * count.sedNum;
			  break;
		default:
		  //n 与 case 1 和 case 2 不同时执行的代码
		}
		$("#firNum").html(count.firNum);
        $("#sedNum").html(count.sedNum);
		//alert('result='+count.result);
	}
	
	//按下enter键
	function press(e){
		
		if(e.keyCode==13)
		{
			countResult(true);
			return false;
		}
	}
	//计算结果
	function countResult(flag){
		if(count.username!=null&&count.sex!=null){
			if(!startFlag){
				startFlag=true;
				//开始计时
				timeNum=100;
				var time=setInterval("refresh();",1000);
				setTimeout("endTimer("+time+");",100000);
                $.ajax({
                    url:"user/startGame",
                    data:{
                        username:count.username,
                    },
                    type:"post",
                    success:function(data){
                    },
                    fail:function(data){
                    }
                });
			}
			if($("#userResult").val() == count.result){
				$("#tip").html("恭喜，答对了<i class='weui-icon-success'></i>");
				if(timeTip!=null)
				clearInterval(timeTip);
				time =setTimeout("$('#tip').html('');",2000);
				$("#userResult").val('');
				count.correctSum++;
				count.countSum++;
				count.continuousCorrect++;
				initCountNum();
			}else if($("#userResult").val()>=10&&count.result<100||flag||$("#userResult").val()>100){
				$("#tip").html("答错了喔<i class='weui-icon-cancel'></i>");
				if(timeTip!=null)
				clearInterval(timeTip);
				time =setTimeout("$('#tip').html('');",2000);
				$("#userResult").val('');
				count.countSum++;
				count.continuousCorrect=0;
				initCountNum();
			}
			if(count.continuousCorrect==10){
				count.continuousCorrect=0;
				//连续答对10道题，增加时间
				timeNum+=5;
				$("#tip").html("不错喔，再接再厉！<i class='weui-icon-success'></i>");
			}
		}else{
			$("#dialog2").show();
			$("#username").focus();
		}
		
	}
	//游戏重新开始
	function rePlay(){					
		$('#dialog1').hide();
	}
	
	function getRandomNum(num){
		return Math.round(Math.random()*num);
	}
	
	//更新时间进度条
	function refresh(){
		timeNum--;
		//$("#percenr").html(timeNum+'%');
		$("#timeProgress").width(timeNum+'%');
	}
	//游戏结束
	function endTimer(obj){
		$("#userResult").attr("disabled",true);
		 clearInterval(obj);
		 $("#loadingToast").show();
		 //$("#percenr").html('100%');
		 
		 //弹出结束对话框
		 if(count.correctSum<40){
			 $("#endContent").html("一共答了"+count.countSum+"道题，答对了"+count.correctSum+"道题，不要灰心，还有很大进步空间呢");
		 }else if(count.correctSum<55){
			 $("#endContent").html("一共答了"+count.countSum+"道题，答对了"+count.correctSum+"道题，还不错哟，继续加油喔");
		 }else if(count.correctSum<75){
			 $("#endContent").html("一共答了"+count.countSum+"道题，答对了"+count.correctSum+"道题，已经很厉害啦，再接再厉！");
		 }else if(count.correctSum<120 && level == -1){
             $("#endContent").html("一共答了"+count.countSum+"道题，答对了"+count.correctSum+"道题，你已经很棒了，试试挑战高难度！");
         }else{
			 $("#endContent").html("一共答了"+count.countSum+"道题，答对了"+count.correctSum+"道题，哇，你是速算天才吗！");
		 }

		 //向服务器反馈
         $.ajax({
         url:"user/updateUser",
         data:{
            username:count.username,
            sex:count.sex,
            countSum:count.countSum,
            correctSum:count.correctSum,
             level:level
         },
         type:"post",
         success:function(data){
             if(data){
                 
            	//重置数据
        		 initCountNum();
        		 $("#userResult").val("");
        		 $("#timeProgress").width('100%');
        		 startFlag=false;
        		 $("#userResult").attr("disabled",false);
        		 count.countSum=0;
        		 count.correctSum=0;
        		 count.continuousCorrect=0;    		 
                 setTimeout("$('#loadingToast').hide();$('#dialog1').show();$('#share').focus();",400);
             }
         },
         fail:function(data){
         }
         });
		 
	}
	
	function share(){
		$('#dialog1').hide();
		document.getElementById('hdsm').style.display='block';
		document.getElementById('fx').style.display='block';
		setTimeout("$('#hdsm').hide();$('#fx').hide()",3000);
	}
	//算术类
    function Count(firNum,sedNum,countSum,correctSum,continuousCorrect)
    {
    	this.firNum=firNum;
        this.sedNum=sedNum;    
        this.countSum=countSum;
        this.correctSum=correctSum;
        this.continuousCorrect=continuousCorrect;//连续答对次数
        this.result;
        this.sex;
        this.username;
    }